import { ApiCall } from "tsrpc";
import { ReqAdmin, ResAdmin } from "../shared/protocols/PtlAdmin";
import adminService from "../services/adminService";
import Admin from "../models/admin";


export default async function (call: ApiCall<ReqAdmin, ResAdmin>) {
    const adminModel = new adminService();

    // 创建数据库
    // adminModel.init();

    // 插入数据库
    // adminModel.create('admin','123456','290850643@qq.com');


    // const admin = await adminModel.findById(1);
    // if (admin) {
    //     console.log('admin not found', admin.id, admin.username, admin.password, admin.email);
    //     call.succ({
    //         id: admin.id,
    //         nickname: admin.username
    //     })
    // }

    const login = await adminModel.login(call.req.username, call.req.password);
    if (!login) {
        // call.error('用户名或密码错误');
        console.log('用户名或密码错误');
        return;
    } else {
        const ret =login as Admin;
        call.succ({
            id: ret.id,
            nickname: ret.email,
        })
    }



}